package cn.joeg.app.web.items.util;


import java.security.MessageDigest;

public class MD5 {

    public static String key = "MT8Wmfa7";

    public static final String MD5(String s) {
        char hexDigits[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
                'E', 'F'
        };

        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str).toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static boolean judge(String str) {
        final int pos = str.indexOf("\"md5\"");
        // logger.debug("pos = " + pos + "  " + str.length());
        String judgeStr;
        if (str.length() <= 14) {
            judgeStr = "";
        } else if (pos < 4) {
            judgeStr = str.substring(pos + 13, str.length() - 1);
        } else if (pos + 13 >= str.length() - 1) {
            judgeStr = str.substring(1, pos - 1);
        } else {
            judgeStr = str.substring(1, pos) + str.substring(pos + 13, str.length() - 1);
        }

        // logger.debug("judgeStr = " + judgeStr);

        String md5Key = MD5(judgeStr + key);
        String md5JudgeStr = md5Key.substring(0, 2)
                             + md5Key.substring(md5Key.length() - 2, md5Key.length());

        String md5Str = str.substring(pos + 7, pos + 11);
        // logger.debug("md5Str = " + md5Str);
        return md5JudgeStr.equals(md5Str);
    }

    public static String generate(String str) {
        String mdKey = MD5(str + key);
        return mdKey.substring(0, 2) + mdKey.substring(mdKey.length() - 2, mdKey.length());
    }

   /* public static String toMD5String(JSONObject obj) {
        String data = obj.toJSONString();
        data = data.substring(1, data.length() - 1);
        String md5Str = generate(data);
        return "{" + data + ",\"md5\":\"" + md5Str + "\"}";
    }
*/
    public static String toMD5String(String str) {
        String md5Str = generate(str);
        return "{" + str + ",\"md5\":\"" + md5Str + "\"}";
    }

    public static void main(String[] args) {
        String input = "{\"gid\":1,\"sid\":0,\"errMsg\":\"测试消息\"}201407241758starwork";
        String md5 = MD5(input);
        System.err.println(md5);

        System.out
                .println(
                        judge("{\"deviceid\":\"ios_7c8d88f29b0f0243d9d9c9a3432faac9b7115d50\",\"md5\":\"80aa\",\"type\":\"withid\"}"));

        // System.out.println(MD5.MD5("20121221"));
        // System.out.println(MD5.MD5("蜂派Hgr9NHuv"));
    }
}
